<template>
  <el-form ref="form" :model="form" :rules="validatorRules" label-width="100px">
    <el-form-item label="用户账号">
      <el-input v-model="form.userId" :disabled="true" readonly/>
    </el-form-item>
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="form.oldPassword" :show-password="true" type="password"/>
    </el-form-item>
    <el-form-item label="登录密码" prop="password">
      <el-input v-model="form.password" :show-password="true" minlength="6" type="password"/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="form.confirmPassword" :show-password="true" minlength="6" type="password"
                @blur="handleConfirmBlur"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitClick">修改密码</el-button>
      <el-button @click="resetForm()">重置</el-button>
    </el-form-item>
    <router-link v-if="false" style="text-decoration: none;" to="/Login">
      <el-dropdown-item>退出登录</el-dropdown-item>
    </router-link>
  </el-form>

</template>


<script>

export default {
  data() {
    return {
      form: {
        userId: '',
        password: '',
        oldPassword: '',
        confirmPassword: ''
      },
      confirmDirty: false,
      validatorRules: {
        oldPassword: [
          {required: true, message: '请输入当前登录密码', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入新登录密码', trigger: 'blur'},
          {min: 6, message: '密码至少为6位数', trigger: 'blur'}
        ],
        confirmPassword: [
          {required: true, message: '请重复输入新登录密码', trigger: 'blur'},
          {validator: this.compareFirstPassword, trigger: 'blur'},
          {min: 6, message: '密码至少为6位数', trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
    this.form.userId = this.$store.getters.getUser.userId;
  },
  methods: {
    submitClick() {
      // 在这里添加密码更新逻辑
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$axios.post('/api/users/updatePassword', this.form, {
            headers: {
              token: this.$store.getters.getToken,
            }
          }).then(response => {
            this.loading = false;
            if (response.data.code === 200) {
              // 根据上传结果执行相应的处理逻辑
              this.$message({message: '修改成功', type: 'success'});
              this.$confirm('密码修改成功，需要重新登录', '提示', {
                confirmButtonText: '重新登录',
                cancelButtonText: '确定',
                type: 'info'
              }).then(() => {
                this.$router.push('/login');  // 执行页面跳转
              }).catch(() => {
                // 用户点击了取消按钮，可以根据需求进行逻辑处理
                this.$router.push('/login');  // 执行页面跳转
              });
            } else {
              this.$message.error(response.data.msg || '修改失败！');
            }
          }).catch(error => {
            // 处理上传失败的情况
            this.loading = false;
            this.$message.error(error || '修改失败！');
          });
        } else {
          return false;
        }
      });
    },
    handleConfirmBlur() {
      this.confirmDirty = true;
    },
    compareFirstPassword(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error('两次输入的密码不一样'));
      } else if (value.length < 6) {
        callback(new Error('密码至少为6位'));
      } else {
        callback();
      }
    },
    resetForm() {
      this.form.userId = this.$store.getters.getUser.userId;
      this.form.password = '';
      this.form.oldPassword = '';
      this.form.confirmPassword = '';
    }
  }
};
</script>


<style scoped>
/* Add your style here */
</style>

